/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ordering.dao;

import com.ordering.dao.exceptions.NonexistentEntityException;
import com.ordering.pojo.TOrderDetail;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import com.ordering.pojo.TOrderHead;
import com.ordering.utils.ConfigIni;

/**
 *
 * @author sulan
 */
public class TOrderDetailJpaController {

    public TOrderDetailJpaController() {
       emf = EntityManagerUtils.em;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(TOrderDetail TOrderDetail) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TOrderHead orderHeadId = TOrderDetail.getOrderHeadId();
            if (orderHeadId != null) {
                orderHeadId = em.getReference(orderHeadId.getClass(), orderHeadId.getId());
                TOrderDetail.setOrderHeadId(orderHeadId);
            }
            em.persist(TOrderDetail);
            if (orderHeadId != null) {
                orderHeadId.getTOrderDetailCollection().add(TOrderDetail);
                orderHeadId = em.merge(orderHeadId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(TOrderDetail TOrderDetail) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TOrderDetail persistentTOrderDetail = em.find(TOrderDetail.class, TOrderDetail.getId());
            TOrderHead orderHeadIdOld = persistentTOrderDetail.getOrderHeadId();
            TOrderHead orderHeadIdNew = TOrderDetail.getOrderHeadId();
            if (orderHeadIdNew != null) {
                orderHeadIdNew = em.getReference(orderHeadIdNew.getClass(), orderHeadIdNew.getId());
                TOrderDetail.setOrderHeadId(orderHeadIdNew);
            }
            TOrderDetail = em.merge(TOrderDetail);
            if (orderHeadIdOld != null && !orderHeadIdOld.equals(orderHeadIdNew)) {
                orderHeadIdOld.getTOrderDetailCollection().remove(TOrderDetail);
                orderHeadIdOld = em.merge(orderHeadIdOld);
            }
            if (orderHeadIdNew != null && !orderHeadIdNew.equals(orderHeadIdOld)) {
                orderHeadIdNew.getTOrderDetailCollection().add(TOrderDetail);
                orderHeadIdNew = em.merge(orderHeadIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = TOrderDetail.getId();
                if (findTOrderDetail(id) == null) {
                    throw new NonexistentEntityException("The tOrderDetail with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TOrderDetail TOrderDetail;
            try {
                TOrderDetail = em.getReference(TOrderDetail.class, id);
                TOrderDetail.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The TOrderDetail with id " + id + " no longer exists.", enfe);
            }
            TOrderHead orderHeadId = TOrderDetail.getOrderHeadId();
            if (orderHeadId != null) {
                orderHeadId.getTOrderDetailCollection().remove(TOrderDetail);
                orderHeadId = em.merge(orderHeadId);
            }
            em.remove(TOrderDetail);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<TOrderDetail> findTOrderDetailEntities() {
        return findTOrderDetailEntities(true, -1, -1);
    }

    public List<TOrderDetail> findTOrderDetailEntities(int maxResults, int firstResult) {
        return findTOrderDetailEntities(false, maxResults, firstResult);
    }

    private List<TOrderDetail> findTOrderDetailEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from TOrderDetail as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public List<TOrderDetail> findTOrderDetailEntitiesBySQL(String sql) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery(sql);
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public TOrderDetail findTOrderDetail(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(TOrderDetail.class, id);
        } finally {
            em.close();
        }
    }

    public int getTOrderDetailCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from TOrderDetail as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
